package com.example.myfirstapp;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Switch;
import android.widget.Toast;
import db.Lista;

public class MainActivity extends Activity {

	public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";

	private DrawerLayout mDrawerLayout;
	private ActionBarDrawerToggle mDrawerToggle;
	private CharSequence mTitle;
	private CharSequence mDrawerTitle;
	private String[] mMenuTitles;
	private ListView mDrawerList;
	private Switch mSwitch;

	
	private ImageView mImageView;
	private FrameLayout layout ;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		// Array con elementos navigation drawer
		mMenuTitles = getResources().getStringArray(R.array.menu_array);
		mDrawerList = (ListView) findViewById(R.id.left_drawer);
		mDrawerList.setAdapter(new ArrayAdapter<String>(this,
				R.layout.drawer_list_item, mMenuTitles));
		mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
		// Icono navigation drawer
		mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
		mDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */
		mDrawerLayout, /* DrawerLayout object */
		R.drawable.ic_drawer, /* nav drawer icon to replace 'Up' caret */
		R.string.drawer_open, /* "open drawer" description */
		R.string.drawer_close /* "close drawer" description */
		) {

			/** Called when a drawer has settled in a completely closed state. */
			public void onDrawerClosed(View view) {
				super.onDrawerClosed(view);
				// mTitle ="My First App";
				getActionBar().setTitle(mTitle);
			}

			/** Called when a drawer has settled in a completely open state. */
			public void onDrawerOpened(View drawerView) {
				super.onDrawerOpened(drawerView);
				mDrawerTitle = "Opciones";
				getActionBar().setTitle(mDrawerTitle);
			}
		};

		// Set the drawer toggle as the DrawerListener
		mDrawerLayout.setDrawerListener(mDrawerToggle);

		getActionBar().setDisplayHomeAsUpEnabled(true);
		// getActionBar().setHomeButtonEnabled(true);
		
		
		//Pruebas
		
		mSwitch = (Switch) findViewById(R.id.switch1);
		mSwitch.setOnCheckedChangeListener(eventoSwitch);

		mImageView = (ImageView) findViewById(R.id.imageView1);
		
		mImageView.setOnClickListener(clickListener);
		layout = (FrameLayout) findViewById(R.id.content_frame);
	}

	
	private OnClickListener clickListener = new OnClickListener() {
	  @SuppressLint("NewApi")
	public void onClick(View v) {
		 
		  		layout.setBackground(mImageView.getDrawable());
		  		
//		  	    requestWindowFeature(Window.FEATURE_NO_TITLE);
//		        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		  		
		  		 getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_IMMERSIVE);
	           }
	};
	
	
	private OnCheckedChangeListener eventoSwitch = new OnCheckedChangeListener() {

		@Override
		public void onCheckedChanged(CompoundButton buttonView,
				boolean isChecked) {
			mImageView = (ImageView) findViewById(R.id.imageView1);
			if(isChecked){
				
				
				mImageView.setVisibility(View.VISIBLE);
			}else{
				mImageView.setVisibility(View.GONE);
			}

		}
	};

	@Override
	protected void onPostCreate(Bundle savedInstanceState) {
		super.onPostCreate(savedInstanceState);
		// Sync the toggle state after onRestoreInstanceState has occurred.
		mDrawerToggle.syncState();
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		mDrawerToggle.onConfigurationChanged(newConfig);
	}

	public void sendMessage(View view) {
		// Toast.makeText(getApplicationContext(), "Me apretaste",
		// Toast.LENGTH_LONG).show();

		// Llamar a un nuevo activity
		// Intent intent = new Intent(this, DisplayMessageActivity.class);
		// EditText editText = (EditText) findViewById(R.id.edit_message);
		// String message = editText.getText().toString();
		// intent.putExtra(EXTRA_MESSAGE, message);
		// startActivity(intent);

		// Dar de alta
		alta(view);

	}

	// @Override
	// public boolean onCreateOptionsMenu(Menu menu) {
	// // Inflate the menu; this adds items to the action bar if it is present.
	// getMenuInflater().inflate(R.menu.main, menu);
	// return true;
	// }
	//
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu items for use in the action bar
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.main_activity_actions, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (mDrawerToggle.onOptionsItemSelected(item)) {
			return true;
		}

		// Handle presses on the action bar items
		switch (item.getItemId()) {
		case R.id.action_search:
			openSearch();
			return true;
		case R.id.action_settings:
			openSettings();
			return true;
		case R.id.action_ayuda:

			Ayuda();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}

	}

	public void Ayuda() {

	}

	public void openSearch() {
		Toast.makeText(getApplicationContext(), "Buscar", Toast.LENGTH_SHORT)
				.show();
	}

	public void openSettings() {
		// Toast.makeText(getApplicationContext(), "Opciones",
		// Toast.LENGTH_SHORT).show();

		Intent intent = new Intent(this, SettingsActivity.class);
		// EditText editText = (EditText) findViewById(R.id.edit_message);
		// String message = editText.getText().toString();
		// intent.putExtra(EXTRA_MESSAGE, message);
		startActivity(intent);

	}


	/* The click listner for ListView in the navigation drawer */
	private class DrawerItemClickListener implements
			ListView.OnItemClickListener {
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			selectItem(position);
		}
	}

	private void selectItem(int position) {
		// update the main content by replacing fragments
		Fragment fragment = null;

		// Toast.makeText(getApplicationContext(),Integer.toString(position),
		// Toast.LENGTH_SHORT).show();
		switch (position) {
		case 0:
			fragment = new NewListFragment();
			break;
		case 1:
			fragment = new ListFragment();

			break;
		case 2:
			openLogin();
			break;
		case 3:
			openFullScreen();
			break;
		case 4:
			openTabs();
			break;
		default:
			break;
		}

		if (fragment != null) {
			FragmentManager fragmentManager = getFragmentManager();
			fragmentManager.beginTransaction()
					.replace(R.id.content_frame, fragment).commit();

		}

		// update selected item and title, then close the drawer
		mDrawerList.setItemChecked(position, true);
		mTitle = (mMenuTitles[position]);
		// setTitle(mMenuTitles[position]);
		mDrawerLayout.closeDrawer(mDrawerList);
		
		 
		
	}

	public void openLogin() {
		Intent intent = new Intent(this, LoginActivity.class);
		startActivity(intent);

	}
	
	public void openFullScreen() {
		Intent intent = new Intent(this, ComprarActivity.class);
		startActivity(intent);

	}
	
	public void openTabs() {
		Intent intent = new Intent(this, ProductosActivity.class);
		startActivity(intent);

	}
	
	
	private EditText et1;
	

	public void alta(View v) {
		InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		  imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
		
		et1 = (EditText) findViewById(R.id.edit_message);
		String nombre = et1.getText().toString();
		
		Lista l = new Lista();
		
		if(l.validar(getApplicationContext(), nombre)){
			l.guardar(getApplicationContext(), nombre);
			Toast.makeText(this, "Se cargo la lista", Toast.LENGTH_SHORT).show();
		}else{
			Toast.makeText(this, "Ya existe una lista con ese nombre", Toast.LENGTH_SHORT).show();
		}
		et1.setText("");
	
	}

}
